// pages/fans/fans.js
let md5 = require("../../utils/md5.js")
let axios = require("../../utils/util.js")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fanstopList: ['关注', '粉丝'],
    fanstopNum: '',
    fansShowList: [],
    confirmShow: false,
    roomid: '',
    password: '', //密码
    input1: '',
  },
  fui_() {
    wx.navigateBack({ //返回
      delta: 1
    })
  },
  changeTop(data) {
    // console.log(data.currentTarget.dataset.index)
    if (data.currentTarget.dataset.index == 0) {
      wx.setNavigationBarTitle({
        title: '关注', //页面标题
      })
    } else if (data.currentTarget.dataset.index == 1) {
      wx.setNavigationBarTitle({
        title: '粉丝', //页面标题
      })
    } else if (data.currentTarget.dataset.index == 2) {
      wx.setNavigationBarTitle({
        title: '收藏', //页面标题
      })
    } else if (data.currentTarget.dataset.index == 3) {
      wx.setNavigationBarTitle({
        title: '足迹', //页面标题
      })
    }
    this.setData({
      fanstopNum: data.currentTarget.dataset.index.toString()
    })
    this.mesChange()
  },
  mesChange() {
    switch (this.data.fanstopNum) {
      case '0': //关注
        this.followList();
        break;
      case '1': //粉丝
        this.fans();
        break;
      case '2': //我的收藏
        this.userVedioList();
        break;
      case '3': //足迹
        this.myVisit();
        break;
    }
  },
  //关注列表
  followList() {
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let _token =
      "userId=" +
      app.globalData.user_id +
      "&token=" +
      app.globalData.userToken +
      "&time=" +
      timestamp +
      "&wPQvCAOU$lbIyg9o2wVg0Dt1k^zy&SaX";
    let params = {
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
      time: timestamp,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/user/followList",
      data: params
    }).then((res) => {
      console.log(res.data.result)
      this.setData({
        fansShowList: res.data.result,
        fanstopNum: '0'
      })
    })
  },
  //粉丝列表
  fans() {
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let _token =
      "userId=" +
      app.globalData.user_id +
      "&token=" +
      app.globalData.userToken +
      "&time=" +
      timestamp +
      "&wPQvCAOU$lbIyg9o2wVg0Dt1k^zy&SaX";
    let params = {
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
      time: timestamp,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/user/fansList",
      data: params
    }).then((res) => {
      console.log(res.data.result)
      this.setData({
        fansShowList: res.data.result,
        fanstopNum: '1'
      })
    })
  },
  //收藏
  userVedioList() {
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let _token =
      "userId=" +
      app.globalData.user_id +
      "&token=" +
      app.globalData.userToken +
      "&time=" +
      timestamp +
      "&page=" +
      '1' +
      "&wPQvCAOU$lbIyg9o2wVg0Dt1k^zy&SaX";
    let params = {
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
      time: timestamp,
      page: '1',
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/index/collectRoom",
      data: params
    }).then((res) => {
      console.log(res.data.result)
      this.setData({
        fansShowList: res.data.result.room_list,
        fanstopNum: '2'
      })
    })
  },
  //足迹
  myVisit() {
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let _token =
      "userId=" +
      app.globalData.user_id +
      "&token=" +
      app.globalData.userToken +
      "&time=" +
      timestamp +
      "&wPQvCAOU$lbIyg9o2wVg0Dt1k^zy&SaX";
    let params = {
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
      time: timestamp,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/user/myVisit",
      data: params
    }).then((res) => {
      console.log(res.data.result)
      this.setData({
        fansShowList: res.data.result,
        fanstopNum: 3
      })
    })
  },
  //取消收藏
  confirmpass_de(data) {
    let room_id = data.currentTarget.dataset.room_id
    this.room_detel(room_id)
  },
  //粉丝
  confirmpass(data) {
    let item = data.currentTarget.dataset.index
    if (item.isFollowEachOther == '0') {
      // console.log("关注");
      this.followInsert(item.userId)
    } else {
      // console.log("取消关注");
      this.followCancel(item.userId)
    }
  },
  //取消收藏房间
  room_detel(e) {
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let arys = {
      roomId: e,
      type: '2',
      userId: app.globalData.user_id,
      time: timestamp,
      token: app.globalData.userToken,
    };
    let _token = axios.encryption(arys);
    let params = {
      roomId: e,
      type: '2',
      userId: app.globalData.user_id,
      time: timestamp,
      token: app.globalData.userToken,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/chat/follow_room",
      data: params
    }).then((res) => {
      wx.showToast({
        title: res.data.text,
        icon: 'none',
        duration: 2000 //持续的时间
      })
      this.userVedioList()
    })
  },
  //进入房间
  to_room(data) {
    let roomid = data.currentTarget.dataset.roomid
    wx.navigateTo({
      url: '/pages/room/room?roomId=' + roomid,
    })
  },
  input1(e) {
    this.setData({
      input1: e.detail.value
    })
  },
  
  //个人主页
  tomemberId(data) {
    wx.navigateTo({
      // <!-- pages/me/me?memberId=892 -->
      url: '/pages/me/me?memberId=' + data.currentTarget.dataset.perfectnumber,
    })
  },
  //取消关注
  confirmpass_(data) {
    let user_id = data.currentTarget.dataset.index
    console.log(user_id);
    let that = this;
    wx.showModal({
      title: '确定要取消关注此用户吗',
      cancelColor: '#333333',
      confirmColor: '#FE804D',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          that.followCancel(user_id)
        }
      }
    })
  },
  //添加关注
  followInsert(followId) {
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let arys = {
      followId: followId,
      userId: app.globalData.user_id,
      time: timestamp,
      token: app.globalData.userToken,
    };
    let _token = axios.encryption(arys);
    let params = {
      followId: followId,
      userId: app.globalData.user_id,
      time: timestamp,
      token: app.globalData.userToken,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/user/followInsert",
      data: params
    }).then((res) => {
      wx.showToast({
        title: res.data.text,
        icon: 'none',
        duration: 2000 //持续的时间
      })
      this.fans()
    })
  },
  //移除关注
  followCancel(followId) {
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let arys = {
      followId: followId,
      userId: app.globalData.user_id,
      time: timestamp,
      token: app.globalData.userToken,
    };
    let _token = axios.encryption(arys);
    let params = {
      followId: followId,
      userId: app.globalData.user_id,
      time: timestamp,
      token: app.globalData.userToken,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/user/followCancel",
      data: params
    }).then((res) => {
      wx.showToast({
        title: res.data.text,
        icon: 'none',
        duration: 2000 //持续的时间
      })
      if (this.data.fanstopNum == '0') {
        this.followList();
      } else if (this.data.fanstopNum == '1') {
        this.fans()
      }
      // console.log(res.data.result)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      fanstopNum: options.fanstopNum
    })
    if (options.fanstopNum == 0) {
      wx.setNavigationBarTitle({
        title: '关注', //页面标题
      })
    } else if (options.fanstopNum == 1) {
      wx.setNavigationBarTitle({
        title: '粉丝', //页面标题
      })
    }
    this.mesChange()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
